package Dao;

import Utils.ESutils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.github.houbb.opencc4j.util.ZhConverterUtil;
import com.google.gson.Gson;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.*;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import pojo.book;

public abstract class BaseDao
{
	public List<book> queryForList(String query,Object... args) {

		List<book> res=new ArrayList<book>();
		System.out.println("连接ES：");
		HttpHost host = new HttpHost("120.25.84.176", 9200);//创建HttpHost
		RestClientBuilder builder = RestClient.builder(host);// 创建RestClientBuilder
		RestHighLevelClient client = new RestHighLevelClient(builder);// 创建RestHighLevelClient

		SearchRequest searchRequest = new SearchRequest();
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//构建查询请求

		if(args[0]!=null){
			//		1.支持分词的条件查询——适用模糊查询

			sourceBuilder.query(QueryBuilders.matchQuery((String) args[0], ZhConverterUtil.toTraditional(query))).query(QueryBuilders.matchQuery((String) args[0], ZhConverterUtil.toTraditional(query)));
//			sourceBuilder.query(QueryBuilders.matchQuery((String) args[0],ZhConverterUtil.toTraditional(query)));


		}else {
			System.out.println("base");
			//5.全字段检索
			QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryStringQuery(ZhConverterUtil.toTraditional(query));
			QueryStringQueryBuilder queryStringQueryBuilder1 = QueryBuilders.queryStringQuery(ZhConverterUtil.toSimple(query));

			sourceBuilder.query(queryStringQueryBuilder).query(queryStringQueryBuilder1);
		}


		//2.不支持分词的条件查询——适用精确查询
		//sourceBuilder.query(QueryBuilders.termQuery("author.keyword", "李淳风"));

		//3.查询全部结果
		//sourceBuilder.query((QueryBuilders.matchAllQuery()));

		//4.复合查询——适用高级查询
//            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//            boolQueryBuilder.must(QueryBuilders.matchQuery("作者", "李淳风"));// 必须包含
//            boolQueryBuilder.mustNot(QueryBuilders.matchQuery("提要", "凡日月"));// 一定不含
//            boolQueryBuilder.should(QueryBuilders.matchQuery("提要", "历代"));// 可能包含
//            sourceBuilder.query(boolQueryBuilder);



		sourceBuilder.size(1000);//最大返回结果
		searchRequest.source(sourceBuilder);
//		searchRequest.indices("shop");//指定索引
		searchRequest.indices("dbclobdemo");

		SearchResponse response = null;
		try {
			response = client.search(searchRequest, RequestOptions.DEFAULT);//获得查询结果
		} catch (IOException e2) {
			e2.printStackTrace();
		}
		SearchHits hits = response.getHits();

		System.out.println("hits========>>");//打印结果
		for (SearchHit hit : hits) {
			Gson gson=new Gson();
//			gson.toJson(hit.getSourceAsString());
			book book1 = gson.fromJson(hit.getSourceAsString(), book.class);
			res.add(book1);

//			System.out.println(hit.getSourceAsString());//输出每条查询的结果信息
		}
		System.out.println("<<========");

		try {
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;

	}
}
